<?php
//
//	file: includes/attach/class_attach_faq.php
//	author: ptirhiik
//	begin: 01/02/2007
//	version: 1.6.1 - 13/06/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

class attach_faq extends plug_ins
{
	function start()
	{
		global $mode;
		if ( $mode != 'attach' )
		{
			return true;
		}

		// regular phpBB
		global $db, $board_config, $template, $lang, $images, $theme, $phpEx, $phpbb_root_path;
		global $userdata, $user_ip;
		global $orig_word, $replacement_word;
		global $starttime;

		// CH
		global $config, $user, $censored_words, $icons, $navigation, $themes, $smilies, $topics_attr, $requester;
		global $forums, $forum_id;

		// get forum_id
		$forum_id = _read(POST_FORUM_URL, TYPE_INT);

		$userdata = session_pagestart($user_ip, $forum_id ? PAGE_POSTING : PAGE_PRIVMSGS);
		$user->set($requester, 'attachcp');

		$attach_rules = new attach_rules($requester);
		$attach_rules->read($forum_id);
		$attach_rules->display();
		unset($attach_rules);

		$gen_simple_header = true;
		$page_title = $user->lang('Attach_rules_title');
		include($config->url('includes/page_header'));
		$template->set_filenames(array('body' => 'attach_rules_body.tpl'));
		$template->pparse('body');
		include($config->url('includes/page_tail'));
		die();
	}

	function get($mode)
	{
		global $config, $user, $faq;
		if ( $mode != 'bbcode' )
		{
			if ( !intval($config->globals['mod_attachment_CH']['config']->data['disable_mod']) && @file_exists(phpbb_realpath($config->url('language/lang_' . $user->lang_used . '/lang_faq_attach'))) )
			{
				include($config->url('language/lang_' . $user->lang_used . '/lang_faq_attach'));
			}
		}
	}
}

class attach_rules
{
	var $requester;
	var $data;
	var $forum_id;

	function attach_rules($requester)
	{
		$this->requester = $requester;
		$this->data = array();
		$this->forum_id = 0;
	}

	function read($forum_id=0)
	{
		global $db, $user, $config;

		$this->data = array();
		$this->forum_id = $forum_id;
		if ( $this->forum_id )
		{
			$user->get_cache(POST_FORUM_URL);
			$authorised = $user->auth(POST_FORUM_URL, 'auth_attachments', $this->forum_id) || $user->auth(POST_FORUM_URL, 'auth_mod', $this->forum_id);
			$_max_filesize = intval($config->globals['mod_attachment_CH']['config']->data['max_filesize']);
		}
		else
		{
			$authorised = ($user->data['user_level'] == ADMIN) || intval($config->globals['mod_attachment_CH']['config']->data['allow_pm_attach']);
			$_max_filesize = intval($config->globals['mod_attachment_CH']['config']->data['max_filesize_pm']);
		}
		if ( $authorised )
		{
			$sql = 'SELECT g.ext_group_id, g.ext_group_name, g.max_filesize, g.forum_permissions, e.extension
						FROM ' . ATT_EXT_GROUPS_TABLE . ' g
							LEFT JOIN ' . ATT_EXTENSIONS_TABLE . ' e
								ON e.ext_group_id = g.ext_group_id
						WHERE g.allow_group = 1
						ORDER BY g.ext_group_name, e.extension';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$extension = $row['extension'];
				unset($row['extension']);
				if ( !isset($this->data[ intval($row['ext_group_id']) ]) )
				{
					$row['extensions'] = array($extension);
					$this->data[ intval($row['ext_group_id']) ] = $row;
				}
				else
				{
					$this->data[ intval($row['ext_group_id']) ]['extensions'][] = $extension;
				}
			}
			$db->sql_freeresult($result);
		}
		else
		{
			message_die(GENERAL_ERROR, 'No_auths_to_rules');
		}

		return true;
	}

	function display()
	{
		global $template, $user, $config;

		// start the display
		if ( !empty($this->data) )
		{
			foreach ( $this->data as $ext_group_id => $row )
			{
				if ( $authorised = (!$this->forum_id || $config->globals['mod_attachment_CH']['extensions']->auth($row['forum_permissions'], $this->forum_id, false)) )
				{
					$units = array();
					$max_filesize = intval($row['max_filesize']);
					$max_filesize_unit = size_get_units($max_filesize, $units);
					$template->assign_block_vars('group', array(
						'GROUP_RULE_HEADER' => sprintf($user->lang('Group_rule_header'), $row['ext_group_name'], round($max_filesize / $max_filesize_unit, 2) . ' ' . $user->lang($units[$max_filesize_unit])),
					));

					// display extensions
					$count_extensions = count($row['extensions']);
					for ( $i = 0; $i < $count_extensions; $i++ )
					{
						$template->assign_block_vars('group.extension', array(
							'EXTENSION' => $row['extensions'][$i],
						));
						$template->set_switch('group.extension.first', $i == 0);
					}
				}
			}
		}

		// send header
		$template->assign_vars(array(
			'L_RULES_TITLE' => $user->lang('Attach_rules_title'),
			'L_EMPTY' => $user->lang('Note_user_empty_group_permissions'),
			'L_CLOSE_WINDOW' => $user->lang('Close_window'),
		));
	}
}

?>